/**
 * Created by vicket on 2017/4/14.
 */
$(function(){
    var username=$("#user_name");
    var password=$("#password");
    var errorText=$(".error_text");
    var registerBtn=$(".register_btn");
    var warningTip=$(".warning_tip");
    //用户名和密码标记
    var isUser=true;
    var isPwd=true;
    username.on("focus",function(){
        errorText.text("");
    });

    username.on("keyup",function(){
        if(!/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(username.val())){
            isUser=false;
            warningTip.text("支持英文、数字、中文");
            setTimeout(function(){
                warningTip.text("");
            },2000)
        }else{
            isUser=true;
        }
    })
    username.on("blur",function(){
        if(username.val().length==0){
            isUser=false;
            warningTip.text("用户名不能为空");
            setTimeout(function(){
                warningTip.text("");
            },2000);
        }else{
            var usernameVal=$("#user_name").val();
            $.ajax({
                type:'post',
                url:"/api/frontRegister",
                data:{username:usernameVal},
                success:function(result){
                    if(result.code==0){
                        isUser=false;
                        warningTip.text(result.message);
                        setTimeout(function(){
                            warningTip.text("");
                        },2000);
                    }else{
                        isUser=true;
                    }
                }
            });
        }
    });
    password.on("focus",function(){
        errorText.text("");
    });
    password.on("blur",function(){
        if(password.val().length==0){
            isPwd=false;
            warningTip.text("密码不能为空");
            setTimeout(function(){
                warningTip.text("");
            },2000);
        }else if(password.val().length<8||password.val().length>20){
            isPwd=false;
            warningTip.text("密码长度是80-20位");
            setTimeout(function(){
                warningTip.text("");
            },2000);
        } else if(!/^[0-9a-zA-Z_]{1,}$/.test(password.val())){
            isPwd=false;
            warningTip.text("密码只支持数字、英文");
            setTimeout(function(){
                warningTip.text("");
            },2000);
        }else{
            isPwd=true;
        }
    });
    registerBtn.tap(function(){
        if(password.val().length==0||username.val().length==0){
            warningTip.text("用户名或密码不能为空");
        }else{
            if(isPwd&&isUser){
                var usernameVal=$("#user_name").val();
                var passwordVal=$("#password").val();
                $.ajax({
                    type:'post',
                    url:"/api/frontRegister",
                    data:{username:usernameVal,password:passwordVal},
                    success:function(data){
                        if(data.code==1){
                            window.location.href="../login.ejs";
                        }else{
                            warningTip.text("注册失败，请重新注册");
                        }
                    }
                })
            }else{
                isLogin=false;
                errorText.text("密码或用户名不正确");
            }

        }
    });
})